
#!/usr/bin/python3
# coding=utf8
import sys
sys.path.append('/home/pi/MasterPi/')
import RPi.GPIO as GPIO

import serial
import threading
import time
import ctypes
 
import math

RECV_LEN = 50

class Gps:
    def __init__(self):
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(7, GPIO.OUT)
        GPIO.output(7, 0)
        self.__ser = serial.Serial('/dev/ttyAMA0', 115200)
        self.__gpsData = [0,0]

 

    def getGpsData(self):
        return self.__gpsData

    def parseData(self,strData):
        arrData = strData.split(',')
        print(arrData)
        if(arrData[0]=='GPGGA'):
            if(bool(arrData[2]) and bool(arrData[4])):
                self.__gpsData = [float(arrData[2])/100,float(arrData[4])/100]
            else:
                self.__gpsData = [0,0]
    
    # Function to receive data
    def receive_data(self):
        isStarted = False
        recvCounts = 0
        allBytes=bytearray()

        while True:
       
            data = self.__ser.read(RECV_LEN)  # Read data from the serial port
      
            if data:
                for index in range(0,RECV_LEN):
                    if data[index]==36:
                        # start recv ...
                        isStarted = True
                        allBytes=bytearray()
             
                    else:
                        if isStarted == True:
                            
                            if data[index]==10:
                                isStarted =False
                                self.parseData(str(allBytes,'utf-8'))

                            allBytes.append(data[index])
                
                    
            

if __name__ == '__main__':
    mySerial = MySerial()
    
    while(True):
        print('runningserai')
        mySerial.receive_data()
        time.sleep(0.1)
